package com.winit.common.filter;

import com.winit.commons.filter.SessionValidFilter;
import com.winit.commons.util.LoginUser;
import com.winit.commons.util.UserInfoUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Order
@WebFilter(filterName = "testFilter1", urlPatterns = "/api/v1/*")
public class CustomerHabitFilter  implements Filter {
    private Logger log = LoggerFactory.getLogger(SessionValidFilter.class);

    private static final List<String> filterUrls = new ArrayList<>();
    static {
        filterUrls.add("/api/v1/issue/getIssues");
        filterUrls.add("/api/v1/brdRequirement/getBrdRequirements");
        filterUrls.add("/api/v1/projectPlan/getProjectPlans");
    }

//    private static final ThreadLocal<Date> timeCount = new ThreadLocal<>();

    public void destroy() {

    }
    @Value("${beetle_auth_url}")
    private String beetle_auth_url;

    @Autowired
    protected HashOperations<String, String, Object> hashOperations;
    @Autowired
    protected RedisTemplate<String, Object> redisTemplate;

//    private String validate_flag;

    public void doFilter(ServletRequest req, ServletResponse res,
                         FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;

        String url = request.getRequestURI();// 用户请求地址
        if (filterUrls.contains(url)) {


            Map queryMap = new HashMap();
            try {
                Enumeration enu = request.getParameterNames();
                while (enu.hasMoreElements()) {
                    String paraName = (String) enu.nextElement();
                    queryMap.put(paraName, request.getParameter(paraName));
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
            queryMap.remove("tokenId");
            if ("1".equals(queryMap.remove("reserveHabit"))){
                String tokenId = request.getParameter("tokenId");
                LoginUser user = UserInfoUtil.getUserInfo(tokenId, beetle_auth_url);
                if (user == null) {
                    log.info("com.winit.commons.filter.SessionValidFilter#doFilter url=" + url + ".查询用户用户不存在,不处理用户搜索习惯.tokenId=" + tokenId);
                }else {
                    hashOperations.put(user.getAccount(), url, queryMap);
                    redisTemplate.expire(user.getAccount(), 60 * 60 * 24 * 7, TimeUnit.SECONDS);
                }
            }

        }

        chain.doFilter(req, res);
    }


    public void init(FilterConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }

}
